package com.honour.controller.interfaced;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.honour.service.interfaced.OrderInManager;
import com.honour.service.interfaced.UserInManager;
import com.honour.service.interfaced.impl.EnergyInService;
import com.system.controller.base.BaseController;
import com.system.util.AppUtil;
import com.system.util.PageData;
import com.system.util.Tools;
import com.system.util.ToolsUtil;




/**
 @Function
 @Author 于泓磊
 @Date   2017年10月29日
*/
@Controller
@RequestMapping(value="/apporderin")
public class OrderInController extends BaseController{
	
	@Resource(name="orderinService")
	private OrderInManager orderinService;
	
	@Resource(name="userinService")
	private UserInManager userinService;
	
	@Resource(name="energyInService")
	private EnergyInService energyInService;
	
	/**
	 * 获得用户名下所有订单列表	
	 * @Author 于泓磊
	 * @date   2017年10月28日
	 * @return
	 */
	@RequestMapping(value="/orderalllist")
	@ResponseBody
	public Object getAllOrderList(){
		logBefore(logger, "获得用户名下所有订单列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("ALLORDER", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("allOrder", pd)){	//检查参数
				List<PageData> list = orderinService.getAllOrderList(pd);
				for (PageData p : list) {
					if (p.get("ORDERSTATUS").equals("0040002")) {
						p.put("ORDERSTATUS", "待收货");
					}else if(p.get("ORDERSTATUS").equals("0040001")){
						p.put("ORDERSTATUS", "待付款");
					}else if(p.get("ORDERSTATUS").equals("0040003")){
						p.put("ORDERSTATUS", "待评价");
					}else {
						p.put("ORDERSTATUS", "已完成");
					}
				}
				if (list.size()!=0) {
					map.put("pd", list);
				}
				result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 获取用户名带条件查询的订单列表（待付款，待收货，待评价，已完成）
	 * @Author 于泓磊
	 * @date   2017年10月28日
	 * @return
	 */
	@RequestMapping(value="/orderconlist")
	@ResponseBody
	public Object getConditionList(){
		logBefore(logger, "获取用户名带条件查询的订单列表（待付款，待收货，待评价，已完成）接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("CONORDER", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("conOrder", pd)){	//检查参数
				List<PageData> list = orderinService.getConditionOrderList(pd);
				if (list.size()!=0) {
				map.put("pd", list);
				}
				result = (0 == list.size()) ?  "02" :  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 购物车列表
	 * @Author 于泓磊
	 * @date   2017年10月30日
	 * @return
	 */
	@RequestMapping(value="/shoppingcart")
	@ResponseBody
	public Object shoppingCart(){
		logBefore(logger, "购物车列表接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHOPPINGCART", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shoppingCart", pd)){	//检查参数
					List<PageData> list = orderinService.getShopCartProList(pd);
					if (list.size()!=0) {
						map.put("pd", list);
						}
						result = (0 == list.size()) ?  "02" :  "01";
					
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 添加商品到购物车
	 * @Author 于泓磊
	 * @date   2017年10月30日
	 * @return
	 */
	@RequestMapping(value="/shoppingcartadd")
	@ResponseBody
	public Object shoppingCartAdd(){
		logBefore(logger, "添加商品到购物车接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPCARTADD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shoppingCartAdd", pd)){	//检查参数
				pd.put("SHOPCART_ID", this.getSnowId());
				orderinService.addShopCartPro(pd);
				//返回01为添加成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 从购物车删除商品
	 * @Author 于泓磊
	 * @date   2017年10月30日
	 * @return
	 */
	@RequestMapping(value="/shoppingcartdel")
	@ResponseBody
	public Object shoppingCartDel(){
		logBefore(logger, "从购物车删除商品接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPCARTDEL", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shoppingCartDel", pd)){	//检查参数
					orderinService.delShopCartPro(pd);
					//返回01为删除成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 生成订单接口（未产生交易）
	 * @Author 于泓磊
	 * @date   2017年10月30日
	 * @return
	 */
	@RequestMapping(value="/shiporder")
	@ResponseBody
	public Object shipOrder(){
		logBefore(logger, "生成订单接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPORDER", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipOrder", pd)){	//检查参数
					pd.put("ORDER_ID", this.getSnowId());
					//调用工具类生成订单号
					String getnum = ToolsUtil.Getnum(pd.getString("HONOURUSER_ID"));
					pd.put("ORDERNUMBER",getnum);
					pd.put("ORDERTIME", ToolsUtil.getStringDate());
					//将订单状态改为待付款
					pd.put("ORDERSTATUS", "0040001");
					//添加相对应的订单
					orderinService.addOrder(pd);
					//添加订单详细到数据库中
					String prolist=(String) pd.get("PRODUCT_ID");
					String[] split = prolist.split(",");
					for (int i = 0; i < split.length; i++) {
						String procountlist=(String) pd.get("PRODUCTCOUNT");
						String[] scount = procountlist.split(",");
						pd.put("ORDERDETAIL_ID", this.getSnowId());
						pd.put("ORDERNUMBER", getnum);
						pd.put("PRODUCT_ID", split[i]);
						if (scount[i].equals(split[i])) {
							pd.put("PRODUCTCOUNT", scount[i]);
						}
					}
					orderinService.addOrderDetails(pd);
					//返回01为创建成功并返回
					result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	/**
	 * 修改订单状态(修改为待收货，待评价，已完成等)
	 * @Author 于泓磊
	 * @date   2017年10月31日
	 * @return
	 */
	@RequestMapping(value="/shiporderstaupd")
	@ResponseBody
	public Object shipOrderStaUpd(){
		logBefore(logger, "修改订单状态接口");
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		String result = "00";
		try{
			if(Tools.checkKey("SHIPORDERUPD", pd.getString("FKEY"))){	//检验请求key值是否合法
				if(AppUtil.checkParam("shipOrderStaUpd", pd)){	//检查参数
				//对订单状态进行修改
				orderinService.updOrderStatus(pd);
				//订单状态为0040003时,获取购物分
				if("0040003".equals(pd.getString("ORDERSTATUS"))){
					//获取参与分值
					Integer score = energyInService.getShareScore(pd);
					//获取原始分值
					PageData energyScore = userinService.getEnergyScore(pd);
					Integer SHOPSCORE=energyScore.getInterger("SHOPSCORE")+score;
					pd.put("SHOPSCORE", SHOPSCORE);
					userinService.updEnergyScore(pd);
				}
				//返回01为修改成功并返回
				result =  "01";
				}else {
					result = "03";
				}
			}else{
				result = "05";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			map.put("result", result);
			logAfter(logger);
		}
		return AppUtil.returnObject(new PageData(), map);
	}
	
	
	
}
